package ar.org.cfired.creditos.gui;

import org.jbpm.api.Configuration;
import org.jbpm.api.ExecutionService;
import org.jbpm.api.ProcessEngine;
import org.jbpm.api.TaskService;
import org.jbpm.api.TaskQuery;
import java.util.List;
import org.jbpm.api.task.Task;
import org.jbpm.api.Execution;

import ar.org.cfired.creditos.gui.action.ActionFactory;
import ar.org.cfired.creditos.gui.action.RechazarAction;
import ar.org.cfired.creditos.parametros.GuardarParametrosException;
public class PruebaRechazo {
	public static void main (String args[]) {
		ProcessEngine proc = new Configuration().buildProcessEngine();
		TaskQuery tq = proc.getTaskService().createTaskQuery();
		ExecutionService ex = proc.getExecutionService();
		Execution e = ex.findExecutionById("Creditos.7967");
		String exId = e.getId();
		String exId2 = exId.substring(0,exId.indexOf("."));
		String resto = exId.substring(exId.indexOf(".") + 1);
		System.out.println("Resto: " + resto);
		if (resto.contains("."))
			exId2 += "." + resto.substring(0,resto.indexOf("."));
		else
			exId2 += "." + resto;
		//exId2 = exId.substring(0,exId.indexOf(".", exId2.length()+1));
		System.out.println(exId2);
		
		
		 proc = new Configuration().buildProcessEngine();
		tq = proc.getTaskService().createTaskQuery();
		tq.processInstanceId(exId2);
		List<Task> tareas = tq.list();
		try {
		RechazarAction accion = (RechazarAction)ActionFactory.getAction("rechazar");
		for (int i=0; i<tareas.size(); i++) {
			Task t = tareas.get(i);
			System.out.println("ID:" + t.getId()); 
			accion.execute(t.getId(), t.getAssignee(), t.getExecutionId());
		}
		}
		catch (GuardarParametrosException exc) {
			
		}
		
		
		
		//Execution e2 = e.getParent();
		//System.out.println(e2.getId());
		//Execution e2 = e.getProcessInstance();
		//System.out.println(e2.getName());
		//System.out.println(e2.getId());
		/*tq.processInstanceId("Creditos.7953");
		List<Task> tareas = tq.list();
		for (int i=0; i<tareas.size(); i++) {
			Task t = tareas.get(i);
			
			System.out.println(t.getId());
		}*/
	}
}
